﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.Security;

namespace BLWCMS.Core.Helpers
{
    public class StringHelper
    {
        #region 密码加密方法，采用MD5双层加密
        /// <summary>
        /// 密码加密方法，采用MD5双层加密
        /// </summary>
        /// <param name="password">原始密码</param>
        /// <returns></returns>
        public static string PasswordEncryption(string password)
        {
            string temp = FormsAuthentication.HashPasswordForStoringInConfigFile(password, "MD5");
            string finalData = FormsAuthentication.HashPasswordForStoringInConfigFile("bolong" + temp, "MD5");

            return finalData;
        }
        #endregion

        #region 过滤字符串中特殊字符方法
        /// <summary>
        /// 过滤字符串中特殊字符方法
        /// </summary>
        /// <param name="html">字符串</param>
        /// <returns></returns>
        public static string CheckStr(string html)
        {
            Regex regex1 =
                  new Regex(@"<script[\s\S]+</script *>",
                  RegexOptions.IgnoreCase);
            Regex regex2 =
                  new Regex(@" href *= *[\s\S]*script *:",
                  RegexOptions.IgnoreCase);
            Regex regex3 =
                  new Regex(@" no[\s\S]*=",
                  RegexOptions.IgnoreCase);
            Regex regex4 =
                  new Regex(@"<iframe[\s\S]+</iframe *>",
                  RegexOptions.IgnoreCase);
            Regex regex5 =
                  new Regex(@"<frameset[\s\S]+</frameset *>",
                  RegexOptions.IgnoreCase);
            Regex regex6 =
                  new Regex(@"\<img[^\>]+\>",
                  RegexOptions.IgnoreCase);
            Regex regex7 =
                  new Regex(@"</p>",
                  RegexOptions.IgnoreCase);
            Regex regex8 =
                  new Regex(@"<p>",
                  RegexOptions.IgnoreCase);
            Regex regex9 =
                  new Regex(@"<[^>]*>",
                  RegexOptions.IgnoreCase);

            html = regex1.Replace(html, ""); //过滤<script></script>标记 
            html = regex2.Replace(html, ""); //过滤href=javascript: (<A>) 属性 
            html = regex3.Replace(html, " _disibledevent="); //过滤其它控件的on...事件 
            html = regex4.Replace(html, ""); //过滤iframe 
            html = regex5.Replace(html, ""); //过滤frameset 
            html = regex6.Replace(html, ""); //过滤frameset 
            html = regex7.Replace(html, ""); //过滤frameset 
            html = regex8.Replace(html, ""); //过滤frameset 
            html = regex9.Replace(html, "");
            html = html.Replace(" ", "");
            html = html.Replace("</strong>", "");
            html = html.Replace("<strong>", "");
            html = html.Replace("'delete", "");
            html = html.Replace("'insert", "");
            html = html.Replace("and", "");
            html = html.Replace("or", "");

            return html;
        }
        #endregion

        #region 字符串加密解密方法

        #region 加密字符串

        /// <summary> /// 加密字符串   
        /// </summary>  
        /// <param name="str">要加密的字符串</param>
        /// <param name="encryptKey">加密密钥</param>   
        /// <returns>加密后的字符串</returns>  
        public static string Encrypt(string str,string encryptKey)
        {
            DESCryptoServiceProvider descsp = new DESCryptoServiceProvider();   //实例化加/解密类对象   

            byte[] key = Encoding.Unicode.GetBytes(encryptKey); //定义字节数组，用来存储密钥    

            byte[] data = Encoding.Unicode.GetBytes(str);//定义字节数组，用来存储要加密的字符串  

            MemoryStream mStream = new MemoryStream(); //实例化内存流对象      

            //使用内存流实例化加密流对象   
            CryptoStream cStream = new CryptoStream(mStream, descsp.CreateEncryptor(key, key), CryptoStreamMode.Write);

            cStream.Write(data, 0, data.Length);  //向加密流中写入数据      

            cStream.FlushFinalBlock();              //释放加密流      

            return Convert.ToBase64String(mStream.ToArray());//返回加密后的字符串  
        }

        #endregion

        #region 解密字符串

        /// <summary>  
        /// 解密字符串   
        /// </summary>  
        /// <param name="str">要解密的字符串</param>
        /// <param name="decryptKey">解密密钥，必须和加密时用的密钥相同</param>  
        /// <returns>解密后的字符串</returns>  
        public static string Decrypt(string str, string decryptKey)
        {
            DESCryptoServiceProvider descsp = new DESCryptoServiceProvider();   //实例化加/解密类对象    

            byte[] key = Encoding.Unicode.GetBytes(decryptKey); //定义字节数组，用来存储密钥    

            byte[] data = Convert.FromBase64String(str);//定义字节数组，用来存储要解密的字符串  

            MemoryStream mStream = new MemoryStream(); //实例化内存流对象      

            //使用内存流实例化解密流对象       
            CryptoStream cStream = new CryptoStream(mStream, descsp.CreateDecryptor(key, key), CryptoStreamMode.Write);

            cStream.Write(data, 0, data.Length);      //向解密流中写入数据     

            cStream.FlushFinalBlock();               //释放解密流      

            return Encoding.Unicode.GetString(mStream.ToArray());       //返回解密后的字符串  
        }

        #endregion
        #endregion

        #region 验证邮箱
        /// <summary>
        /// 验证邮箱
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static bool IsEmail(string source)
        {
            return Regex.IsMatch(source, @"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$", RegexOptions.IgnoreCase);
        }
        public static bool HasEmail(string source)
        {
            return Regex.IsMatch(source, @"[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})", RegexOptions.IgnoreCase);
        }
        #endregion

    }
}
